﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Pickers
{
    public partial class SeviceItemPicker : UserControl, IView
    {
        private int _pageIndex;
        private int _pageCount;
        Promise _promise = new Promise();

        public SeviceItemPicker()
        {
            InitializeComponent();
            ctlGrid.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("confirm", Resources.isOK, "确定", btnConfirm_Click))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_Click))
                .AddAction(new ActionNode("cancel", Resources.isDelete, "取消", btnCancel_Click));
        }

        public void Active(ViewActiveArgs args)
        {
            _promise.Then(LoadServiceCategoryListData)
                .Then(LoadServiceItemListData)
                .Run(null);
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("选择服务项目");
        }

        #endregion

        #region Method
        /// <summary>
        /// 绑定服务类别
        /// </summary>
        private void LoadServiceCategoryListData()
        {
            this.ShowMask("Loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.EServiceCategory> result = new Service.QueryService().QueryServiceCategoryPagedList(ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId, Domain.Constant.CommonStatus.ALL, 0, Domain.Constant.PageLimitAll);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.ctlLookupCategory.Properties.DisplayMember = "Name";
                        this.ctlLookupCategory.Properties.ValueMember = "Id";
                        this.ctlLookupCategory.Properties.DataSource = result.Data.Items;
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        /// <summary>
        /// 绑定服务项目
        /// </summary>
        private void LoadServiceItemListData()
        {
            this.ShowMask("Loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VServiceItem> result = new Service.QueryService().QueryServiceItemPagedList(
                    this.ctlEditQry.Text.Trim(),
                    GetSelectedCategoryId(),
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    Domain.Constant.CommonStatus.Enabled,
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.ctlGrid.Paginate<WebRef.VServiceItem>(result, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }

        private Guid? GetSelectedCategoryId()
        {
            WebRef.EServiceCategory category = ctlLookupCategory.GetSelectedDataRow() as WebRef.EServiceCategory;
            if (category != null)
            {
                return category.Id;
            }
            return null;
        }

        private int GetPageLimit()
        {
            return 25;
        }
        #endregion

        #region Action
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, object e)
        {
            int[] selRows = this.GVData.GetSelectedRows();
            if (selRows == null || selRows.Length == 0)
            {
                return;
            }
            WebRef.VServiceItem vServiceItem = GVData.GetRow(selRows[0]) as WebRef.VServiceItem;
            UIContext.Current.ViewManager.CloseView(true, vServiceItem);
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, object e)
        {
            _pageIndex = 0;
            LoadServiceItemListData();
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlLookupCategory_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            string tagString = Convert.ToString(e.Button.Tag);
            if (string.Equals(tagString, "clear"))
            {
                ctlLookupCategory.EditValue = null;
            }
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            LoadServiceItemListData();
        }
        /// <summary>
        /// 双击行选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GVData_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = GVData.CalcHitInfo(new Point(e.X, e.Y));
                if (hInfo.InRow)
                {
                    if (GVData.FocusedRowHandle > -1)
                    {
                        WebRef.VServiceItem supplier = GVData.GetFocusedRow() as WebRef.VServiceItem;
                        UIContext.Current.ViewManager.CloseView(true, supplier);
                    }
                }
            }
        }
        #endregion

    }
}
